import java.util.*;

class CheckupData {
    String checkupDate; // 检查时间，格式为 "yyyy-MM-dd"
    String institution; // 检查机构
    String item; // 检查项目

    public CheckupData(String checkupDate, String institution, String item) {
        this.checkupDate = checkupDate;
        this.institution = institution;
        this.item = item;
    }

    public Date getDate() throws Exception {
        return new java.text.SimpleDateFormat("yyyy-MM-dd").parse(this.checkupDate);
    }
}

public class RepeatCheckupDetector {

    public static boolean check_match(String item1, String item2) {
        // 示例实现：这里你可以调用实际的第三方函数或API来比较两个检查项目是否匹配
        return item1.equals(item2); // 示例中只是简单地用equals进行比较
    }

    public static boolean isRepeatCheckup(List<CheckupData> checkupDataList, int N) throws Exception {
        Map<String, List<CheckupData>> itemToCheckupsMap = new HashMap<>();

        // 分组步骤
        for (CheckupData data : checkupDataList) {
            boolean added = false;
            for (String existingItem : itemToCheckupsMap.keySet()) {
                if (check_match(existingItem, data.item)) {  // 使用第三方函数比较项目
                    itemToCheckupsMap.get(existingItem).add(data);
                    added = true;
                    break;
                }
            }
            if (!added) {
                itemToCheckupsMap.computeIfAbsent(data.item, k -> new ArrayList<>()).add(data);
            }
        }

        // 检查重复步骤
        for (List<CheckupData> checkups : itemToCheckupsMap.values()) {

                checkups.sort(Comparator.comparing(checkupData -> {
                    try {
                        return checkupData.getDate();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }));

            Deque<CheckupData> deque = new LinkedList<>();
            Set<String> institutionsSet = new HashSet<>(); // 用于存储当前窗口中的机构名称

            for (CheckupData currentData : checkups) {
                Date currentDate = currentData.getDate();

                // 移除超过时间窗口的数据
                while (!deque.isEmpty()) {
                    CheckupData oldestData = deque.peekFirst();
                    long daysDiff = (currentDate.getTime() - oldestData.getDate().getTime()) / (1000 * 60 * 60 * 24);

                    if (daysDiff > N) {
                        deque.pollFirst();
                        institutionsSet.remove(oldestData.institution); // 同时从集合中移除过期机构
                    } else {
                        break;
                    }
                }

                // 添加当前数据的机构到集合中
                institutionsSet.add(currentData.institution);

                // 如果集合的大小大于1，说明有重复检查
                if (institutionsSet.size() > 1) {
                    return true;
                }

                deque.addLast(currentData);
            }
        }

        return false;
    }

    public static void main(String[] args) throws Exception {
        List<CheckupData> checkupDataList = new ArrayList<>();
        checkupDataList.add(new CheckupData("2024-08-01", "Hospital A", "Blood Test"));
        checkupDataList.add(new CheckupData("2024-08-02", "Hospital B", "Blood Test"));
        checkupDataList.add(new CheckupData("2024-08-03", "Hospital A", "X-Ray"));
        checkupDataList.add(new CheckupData("2024-08-10", "Hospital C", "Blood Test"));

        int N = 2; // 时间差为2天

        boolean result = isRepeatCheckup(checkupDataList, N);
        System.out.println("是否存在重复检查: " + result);
    }
}
